package controllers;

import java.util.List;

import libs.Validation;
import models.User;
import models.forms.LoginForm;
import models.forms.SignupForm;
import play.data.Form;
import play.data.validation.ValidationError;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.index;
import views.html.user.login;
import views.html.user.signup;
import business.UserBusiness;

public class Application extends Controller {

	public static Result index() {
		return ok(index.render("Your new application is ready."));
	}

	public static Result login() {
		Form<LoginForm> loginForm = Form.form(LoginForm.class);
		if (Validation.isPost()) {
			Form<LoginForm> filledForm = loginForm.bindFromRequest();
			if (filledForm.hasErrors()) {
				return badRequest(login.render(loginForm));
			} else {
				LoginForm form = filledForm.get();
				UserBusiness userBusiness = new UserBusiness();
				User user = userBusiness.login(form);
				if(user != null){
					session("userId", String.valueOf(user.getId()));
					session("userName", user.getUsername());
					session("userFullName", user.getFullName());
					return redirect(routes.Application.index());
				}else{
					filledForm.reject("username", "Login failed! Your username or password is invalid!");
				}
			}
		}
		return ok(login.render(loginForm));
	}

	public static Result signup() {
		Form<SignupForm> signupForm = Form.form(SignupForm.class);
		if (Validation.isPost()) {
			Form<SignupForm> filledForm = signupForm.bindFromRequest();
			try {
				// Check accept conditions
				/*if (!"true".equals(filledForm.field("accept").value())) {
					filledForm.reject("accept", "You must accept the terms and conditions");
				}*/

				// Check username conditions
				UserBusiness userBusiness = new UserBusiness();
				if (!filledForm.field("username").valueOr("").isEmpty()) {
					if (userBusiness.checkExistByUsername(filledForm.field("username").value())) {
						filledForm
								.reject("username", "Your username is exists on the system. Please choose other one!");
					}
				}

				// Check repeated password
				if (!filledForm.field("password").valueOr("").isEmpty()) {
					if (!filledForm.field("password").valueOr("").equals(filledForm.field("repeatPassword").value())) {
						filledForm.reject("repeatPassword", "Password don't match");
					}
				}

				if (filledForm.hasErrors()) {
					for (String keyError : filledForm.errors().keySet()) {
						List<ValidationError> lstErrors = filledForm.errors().get(keyError);
						for (ValidationError error : lstErrors) {
							System.out.println("Error " + keyError + ": " + error.message().toString());
						}
					}
					return badRequest(signup.render(signupForm));
				} else {
					SignupForm form = filledForm.get();
					userBusiness.save(form);
				}
			} catch (Exception e) {
				System.out.println("Not binding request!--------------");
			}
		}
		return ok(signup.render(signupForm));
	}

	public static Result logout(){
		// session().clear();
		return ok(views.html.logout.render());
	}
}
